package com.acyouzi.leetcode.array;

import java.util.HashMap;

/**
 * 17/9/20 21:23
 *
 * @author sunxu
 */
public class ContiguousArray {
  public static int findMaxLength(int[] nums) {
    int sum = 0;
    int max = 0;
    HashMap<Integer, Integer> map = new HashMap<>();
    map.put(0,-1);
    for (int i = 0; i < nums.length; i++) if (nums[i] == 0) nums[i] = -1;
    for (int i = 0; i < nums.length; i++) {
      sum += nums[i];
      if (map.containsKey(sum)){
        max = Math.max(max,i - map.get(sum));
      }else{
        map.put(sum,i);
      }
    }
    return max;
  }

  public static void main(String[] args) {
    findMaxLength(new int[]{0,1,0});
  }
}
